package com.example.database.library;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import android.content.Context;

public class UserFunctions {

	private JSONParser jsonParser;

	private static String api_key = "nmc-android";

	// private static String LoginURL = "http://10.0.2.2/android_booklib/";
	// private static String RegisterURL = "http://10.0.2.2/android_booklib/";
	// private static String forgPassURL = "http://10.0.2.2/android_booklib/";

	private static String LoginURL = "http://bookstore-sf14.l:8080/api/"
			+ api_key + "/auth/signin.json";
	private static String RegisterURL = "http://bookstore-sf14.l:8080/api/"
			+ api_key + "/auth/signup.json";
	private static String forgPassURL = "http://bookstore-sf14.l:8080/api/"
			+ api_key + "/auth/forgotPassword.json";

	// private static String LoginURL =
	// "http://10.0.2.2/android_booklib/signin.json";

	private static String login_tag = "login";
	private static String register_tag = "register";
	private static String passforg_tag = "passforg";

	// constructor
	public UserFunctions() {
		jsonParser = new JSONParser();

	}

	public JSONObject loginUser(String email, String password) {

		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("username", email));
		params.add(new BasicNameValuePair("password", password));
		JSONObject json = jsonParser.getJSONFromUrl(LoginURL, params);

		// Log.e("JSON", params.toString());
		return json;
	}

	public JSONObject registerUser(String name, String email, String password) {

		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("username", name));
		params.add(new BasicNameValuePair("email", email));
		params.add(new BasicNameValuePair("password", password));
		JSONObject json = jsonParser.getJSONFromUrl(RegisterURL, params);
		return json;
	}

	public JSONObject forgotPassword(String email) {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("email", email));

		JSONObject json = jsonParser.getJSONFromUrl(forgPassURL, params);
		return json;
	}

	public boolean isUserLoggedIn(Context context) {
		DatabaseHandler db = new DatabaseHandler(context);
		int count = db.getRowCount();
		if (count > 0) {
			// user logged in
			return true;
		}
		return false;
	}

	public JSONObject ratingbar1(String r) {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("tag", passforg_tag));
		params.add(new BasicNameValuePair("email", r));

		JSONObject json = jsonParser.getJSONFromUrl(forgPassURL, params);
		return json;
	}

	public boolean logoutUser(Context context) {
		DatabaseHandler db = new DatabaseHandler(context);
		db.resetUserTables();
		return true;
	}

}
